#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
    int data;
    struct Node *next;
}Node;
Node* createNode(int data){
    Node* newNode=(Node*)malloc(sizeof(Node));
    if (newNode == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        exit(EXIT_FAILURE);
    }
    newNode->data=data;
    newNode->next=NULL;
    return newNode;
}
Node* bothList(Node* list1,Node* list2){
    Node* list=NULL;
    Node** last=&list;
    while (list1 != NULL && list2 != NULL) {
        if (list1->data < list2->data) {
            list1 = list1->next;
        } else if (list1->data > list2->data) {
            list2 = list2->next;
        } else { 
            *last = createNode(list1->data);
            last = &((*last)->next);
            list1 = list1->next;
            list2 = list2->next;
        }
    }
    return list;
}
void printList(Node* list) {
    if (list == NULL) {
        printf("NULL\n");
        return;
    }
    Node* current = list;
    while (current != NULL) {
        printf("%d", current->data);
        current = current->next;
        if (current != NULL) {
            printf(" ");
        }
    }
    printf("\n");
}
void freeList(Node* list) { // 释放链表内存
    Node* current = list;
    while (current != NULL) {
        Node* next = current->next;
        free(current);
        current = next;
    }
}
int main(){
    Node* list1=NULL;
    Node* list2=NULL;
    Node** last1=&list1;
    Node** last2=&list2;
    int a;
    while(scanf("%d",&a)&&a!=-1){
        *last1=createNode(a);
        last1=&((*last1)->next);
    }
    while(scanf("%d",&a)&&a!=-1){
        *last2=createNode(a);
        last2=&((*last2)->next);
    }
    Node* list=bothList(list1,list2);
    printList(list);
    
    freeList(list1);
    freeList(list2);
    freeList(list);
    return 0;
}